<template>
  <!-- 采购退货编辑 -->
  <div>
    <FormPanel ref="form" :cols="3" :list="formList" :isSubmitBtn="false" @formChange="formChange"></FormPanel>
    <el-row>
      <div class="box_02">
        <el-button size="small" icon="el-icon-folder-add" type="primary" @click="confirm()">保存</el-button>
        <el-button size="small" icon="el-icon-refresh-left" type="primary" @click="$emit('cancel')">取消</el-button>
      </div>
    </el-row>
  </div>
</template>

<script>
import { state, mutations } from './../store';

export default {
  name: 'cgrk_return_edit',
  props: {
    formData: Object
  },
  data() {
    return {
      formList: this.createFormList()
    };
  },
  computed: {
    tax() {
      return state.tax;
    }
  },
  mounted() {
    window.cgrk_return_edit = this;
    console.log(this.formData);
    this.formList.forEach(item => {
      item.initialValue = this.formData[item.fieldName];
    });
  },
  methods: {
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '仓库',
          fieldName: 'STORAGE_NAME', // STORAGE_CODE   原
          disabled: true
        },
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'PART_NO',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'PART_NAME',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '可退货数量',
          fieldName: 'CAN_RETURN_QUANTITY',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '退货数量',
          fieldName: 'IN_QUANTITY',
          change: e => {
            if (isNaN(Number(e))) {
              this.formList.find(item => item.fieldName == 'IN_QUANTITY').initialValue = 0;
            }
          }
        }
      ];
    },
    // 确定按钮
    confirm() {
      // 收集表单数据 触发 formChange方法
      this.$refs.form.SUBMIT_FORM();
    },
    // 计算价格
    computP1(row) {
      const sl = Number(state.tax);
      console.log('税率', sl);
      // 数量
      const IN_QUANTITY = row.IN_QUANTITY;
      // 入库 不含税单价
      const IN_PRICE = row.IN_PRICE;

      // 入库含税单价
      const IN_PRICE_TAXED = Number(IN_PRICE) * (sl + 1);
      row.IN_PRICE_TAXED = IN_PRICE_TAXED.toFixed(2);
      // 入库不含税金额
      const IN_AMOUNT = Number(IN_QUANTITY) * Number(IN_PRICE);
      row.IN_AMOUNT = IN_AMOUNT.toFixed(2);
      // 入库含税金额
      const IN_AMOUNT_TAXED = Number(IN_QUANTITY) * Number(IN_PRICE_TAXED);
      row.IN_AMOUNT_TAXED = IN_AMOUNT_TAXED.toFixed(2);
    },
    formChange(val) {
      if (Number(val.IN_QUANTITY) <= Number(val.CAN_RETURN_QUANTITY)) {
        if (Number(val.IN_QUANTITY) > 0.01) {
          const row = Object.assign({}, this.formData, val);
          console.log(row, 'row的数据');
          // 这里计算
          const sl = Number(state.tax);
          console.log('税率', sl);
          // 数量
          const IN_QUANTITY = row.IN_QUANTITY;
          // 入库 不含税单价
          const IN_PRICE = row.IN_PRICE;
          // 入库含税单价
          const IN_PRICE_TAXED = Number(IN_PRICE) * (sl + 1);
          row.IN_PRICE_TAXED = IN_PRICE_TAXED.toFixed(2);
          // 入库不含税金额
          const IN_AMOUNT = Number(IN_QUANTITY) * Number(IN_PRICE);
          row.IN_AMOUNT = IN_AMOUNT.toFixed(2);
          // 入库含税金额
          const IN_AMOUNT_TAXED = Number(IN_QUANTITY) * Number(IN_PRICE_TAXED);
          row.IN_AMOUNT_TAXED = IN_AMOUNT_TAXED.toFixed(2);
          // 这里计算结束
          if (row.IN_AMOUNT_TAXED != 0) {
            row.IN_AMOUNT_TAXED = -row.IN_AMOUNT_TAXED;
          }
          if (row.IN_AMOUNT != 0) {
            row.IN_AMOUNT = -row.IN_AMOUNT;
          }
          if (row.IN_QUANTITY != 0) {
            row.IN_QUANTITY = -row.IN_QUANTITY;
          }
          this.$emit('confirm', row);
        } else {
          this.$notify({
            title: '警告',
            message: '请输入不小于0.01的数值',
            type: 'warning'
          });
        }
      } else {
        this.$notify({
          title: '警告',
          message: '退货数量不能大于可退货数量',
          type: 'warning'
        });
      }
    }
  }
};
</script>
<style lang="less" scoped>
.box_02 {
  text-align: center;
  margin-top: 20px;
}
</style>
